# 
# Steven W. Webster
# "Anger and Declining Trust in Government in the American Electorate"
# Sentiment analysis replication code
#
# Note that the sentiment analysis itself was conducted using the 
# Linguistic Inquiry Word Count (LIWC), a third-party piece of software.
# The results were then imported into R for further analysis. The
# data required for this portion of the paper are the results derived
# from the LIWC. For more see, www.liwc.net.
#

setwd("")

library(dplyr)
library(stargazer)
library(ggplot2)

main <- read.csv("angerid.csv")
anger <- read.csv("anger_only_results.csv")
anger_politics <- read.csv("anger_politics_results.csv")
salience <- read.csv("salience_results.csv")
control <- read.csv("control_results.csv")

anger <- rename(anger, text = anger)
anger_politics <- rename(anger_politics, text = anger_politics)
salience <- rename(salience, text = salience)
control <- rename(control, text = breakfast_control)

liwc <- bind_rows(anger, anger_politics, salience, control)

main2 <- left_join(main, liwc, by = "id")

# MODELS
# pct of angry words on treatment status 
m1 <- lm(angerscore ~ anger_treated + anger_salience_treated + salience_treated, data = main2)
# pct of negative emotion words on treatment status
m2 <- lm(negemo ~ anger_treated + anger_salience_treated + salience_treated, data = main2)
# pct of positive emotion words on treatment status
m3 <- lm(posemo ~ anger_treated + anger_salience_treated + salience_treated, data = main2)

out <- list(m1, m2, m3)

stargazer(out,
          type = "latex",
          style = "apsr",
          title = "Sentiment Analysis by Treatment Status",
          dep.var.labels = c("Pct. Angry Words","Pct. Negative Emotions","Pct. Positive Emotions"),
          covariate.labels = c("Angry","Angry about politics","Think about politics"),
          model.numbers = F,
          keep.stat = c("n","rsq"),
          out = "",
          label = "tab:sentiment-analysis"
)

# Plot distributions
plot.dat <- main2 %>%
  select(angerscore, pid7, anger_treated, anger_salience_treated, salience_treated, control)

plot.dat$Treatment[plot.dat$anger_treated==1] <- "Anger"
plot.dat$Treatment[plot.dat$anger_salience_treated==1] <- "Angry about politics"
plot.dat$Treatment[plot.dat$salience_treated==1] <- "Think about politics"
plot.dat$Treatment[plot.dat$control==1] <- "Control"

plot.dat <- filter(plot.dat, Treatment != "Control")

# remove the 5% in the right tail for presentation purposes
plot.dat2 <- filter(plot.dat, angerscore < 11) 

plot.dat2 %>%
  group_by(treatment) %>%
  summarize(means = mean(angerscore))

p1 <- ggplot(plot.dat2, aes(angerscore, color = Treatment)) + geom_density()
p1 <- p1 + theme_bw() + ylab("Density") + xlab("Pct. Angry Words")
p1 <- p1 + theme(legend.position = "bottom")
ggsave("anger-distributions.pdf")


plot.dat <- main2 %>%
  select(negemo, pid7, anger_treated, anger_salience_treated, salience_treated, control)

plot.dat$Treatment[plot.dat$anger_treated==1] <- "Anger"
plot.dat$Treatment[plot.dat$anger_salience_treated==1] <- "Angry about politics"
plot.dat$Treatment[plot.dat$salience_treated==1] <- "Think about politics"
plot.dat$Treatment[plot.dat$control==1] <- "Control"

plot.dat <- filter(plot.dat, Treatment != "Control")

plot.dat2 <- filter(plot.dat, negemo < 15) 


p2 <- ggplot(plot.dat2, aes(negemo, color = Treatment)) + geom_density()
p2 <- p2 + theme_bw() + ylab("Density") + xlab("Pct. Negative Emotional Words")
p2 <- p2 + theme(legend.position = "bottom")
ggsave("pct-negemo.pdf")
